import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import ColumnDetail from '@/components/ColumnDetail.vue'
import CreatPost from './views/CreatPost.vue'
import store from './store'
import PostDetail from './views/PostDetail.vue'
// import SignUp from './views/Signup.vue'
const routerHistory = createWebHistory()
const router = createRouter({
  history: routerHistory, // history模式
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login,
      meta: { readylogin: true }
    },
    {
      path: '/Column/:id',
      name: 'Column',
      component: ColumnDetail
    },
    {
      path: '/Create',
      name: 'Create',
      component: CreatPost,
      meta: { requiredlogin: true }
    },
    {
      path: '/posts/:id',
      name: 'post',
      component: PostDetail
    }
  ]

})
router.beforeEach((to, from, next) => {
  if (to.meta.requiredlogin && !store.state.user.isLogin) {
    next({ name: 'Login' })
  } else if (to.meta.readylogin && store.state.user.isLogin) {
    next('/')
  } else {
    next()
  }
}
)
export default router
